package com.hnu.seckill.controller;


import com.hnu.seckill.entity.TUser;
import com.hnu.seckill.service.ITGoodsService;
import com.hnu.seckill.service.IUserService;
import com.hnu.seckill.vo.DetailVo;
import com.hnu.seckill.vo.GoodsVo;
import com.hnu.seckill.vo.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/goods")
@Slf4j
public class GoodsController {
    @Autowired
    IUserService userService;
    @Autowired
    ITGoodsService goodsService;
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
    /**
     * 跳转商品列表页
     * windows优化前QPS：2153/sec 100000次
     *        缓存 QPS：7500/sec 100000次
     * @param model
     * @param TUser
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/goodslist",produces = "text/html;charset=utf-8")
    public String goodslist(Model model, TUser TUser, HttpServletRequest request, HttpServletResponse response) {

        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html = (String) valueOperations.get("goodslist");
        if(!StringUtils.isEmpty(html)){
            return html;
        }
        model.addAttribute("user", TUser);
        model.addAttribute("goodsList", goodsService.findGoodsVo());
        WebContext webContext = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goodslist",webContext);
        if(!StringUtils.isEmpty(html)){
            valueOperations.set("goodslist",html,1, TimeUnit.MINUTES);
        }
        return html;
    }
    @ResponseBody
    @RequestMapping(value="/goodsDetail2/{goodsId}",produces = "text/html;charset=utf-8")
    public String Detail2(@PathVariable long goodsId, Model model, TUser user,HttpServletRequest request, HttpServletResponse response) {

        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html = (String) valueOperations.get("goodsDetail:"+goodsId);
        if(!StringUtils.isEmpty(html)){
            return html;
        }

        model.addAttribute("user", user);
        GoodsVo goodsVo = goodsService.findGoodsVobyGoodsId(goodsId);
        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        Date nowDate = new Date();
        int secKillStatus = 0;
        int remainSeconds = 0;
        if (nowDate.before(startDate)) {
            secKillStatus = 0;
            remainSeconds = (int) (startDate.getTime() - nowDate.getTime()) / 1000;
        } else if (nowDate.after(endDate)) {
            secKillStatus = 2;
            remainSeconds = -1;
        } else {
            secKillStatus = 1;
            remainSeconds = 0;
        }
//        System.out.println(secKillStatus);
        model.addAttribute("secKillStatus", secKillStatus);
        model.addAttribute("remainSeconds", remainSeconds);
        model.addAttribute("goods", goodsVo);

        WebContext webContext = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goodsDetail",webContext);
        if(!StringUtils.isEmpty(html)){
            valueOperations.set("goodsDetail:",html,1, TimeUnit.MINUTES);
        }
        return html;
    }





    @ResponseBody
    @RequestMapping("/goodsDetail/{goodsId}")
    public RespBean Detail(@PathVariable long goodsId, TUser user, HttpServletRequest request, HttpServletResponse response) {


//        model.addAttribute("user", user);
        GoodsVo goodsVo = goodsService.findGoodsVobyGoodsId(goodsId);
        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        Date nowDate = new Date();
        int secKillStatus = 0;
        int remainSeconds = 0;
        if (nowDate.before(startDate)) {
            secKillStatus = 0;
            remainSeconds = (int) (startDate.getTime() - nowDate.getTime()) / 1000;
        } else if (nowDate.after(endDate)) {
            secKillStatus = 2;
            remainSeconds = -1;
        } else {
            secKillStatus = 1;
            remainSeconds = 0;
        }

        DetailVo detailVo = new DetailVo(user,goodsVo,secKillStatus,remainSeconds);

        return RespBean.success(detailVo);
    }
}
